<template>
  <el-container class="help-category">
    <!-- 顶部导航菜单 -->
    <div class="nav-menu">
      <el-dropdown trigger="click">
        <span class="el-dropdown-link">
          目录 <i class="el-icon-arrow-down el-icon--right" />
        </span>
        <el-dropdown-menu slot="dropdown">
          <el-dropdown-item @click.native="scrollToContent('install')">
            安装指南
          </el-dropdown-item>
          <el-dropdown-item @click.native="scrollToContent('faq')">
            常见问题
          </el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
    </div>

    <!-- 主体内容 -->
    <el-main>
      <el-card id="encryption" class="card-item">
        <template #header>
          <div class="card-header">
            <h2>认识激活以及激活过程</h2>
          </div>
        </template>
        <MarkdownWithHighlight :source="encryptionContent" />
      </el-card>
    </el-main>

    <!-- 回到顶部按钮 -->
    <div class="back-to-top" @click="scrollToTop">
      置顶<el-button type="text" icon="el-icon-arrow-up" />
    </div>
  </el-container>
</template>

<script>
import MarkdownWithHighlight from '@/components/MarkdownWithHighlight';

export default {
  name: 'HelpCategory',
  components: {
    MarkdownWithHighlight
  },
  data() {
    return {
      installContent: '', // 安装指南内容
      faqContent: '', // 常见问题内容
      encryptionContent: ''
    };
  },
  async mounted() {
    // 加载 Markdown 文档内容
    await this.loadMarkdownContent();
  },
  methods: {
    // 加载 Markdown 文档内容
    async loadMarkdownContent() {
      try {
        this.installContent = await this.fetchMarkdown('/docs/install.md');
        this.faqContent = await this.fetchMarkdown('/docs/faq.md');
        this.encryptionContent = await this.fetchMarkdown('/docs/encryption.md');
      } catch (error) {
        console.error('加载 Markdown 文档失败:', error);
      }
    },
    async fetchMarkdown(url) {
      const response = await fetch(url);
      if (!response.ok) {
        throw new Error(`HTTP error! status: ${response.status}`);
      }
      return await response.text();
    },
    scrollToContent(id) {
      // 滚动到对应的内容区域
      const element = document.getElementById(id);
      if (element) {
        element.scrollIntoView({ behavior: 'smooth' });
      }
    },
    scrollToTop() {
      // 滚动到页面顶部
      window.scrollTo({
        top: 0,
        behavior: 'smooth'
      });
    },
  },
};
</script>

<style scoped>
.help-category {
  padding: 20px;
  min-height: 100vh;
  /* 确保容器占满整个视口高度 */
  display: flex;
  flex-direction: column;
  position: relative;
  /* 确保子元素可以相对于父元素定位 */
}

.nav-menu {
  margin-bottom: 20px;
  /* 增加一些下边距，使导航菜单与主体内容分开 */
}

.el-dropdown-link {
  cursor: pointer;
  color: #409EFF;
}

.el-dropdown-menu {
  min-width: 160px;
}

.el-main {
  padding: 20px;
  flex: 1;
  /* 主体内容占据剩余空间 */
  overflow-y: auto;
  /* 允许垂直滚动 */
}

.card-item {
  margin-bottom: 20px;
  border-radius: 8px;
  padding-left: 25px;
  padding-right: 25px;
  width: 75%;
  margin-left: 10%;
  /* 向右偏移 */
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.card-header h2 {
  margin: 0;
  font-size: 20px;
  font-weight: bold;
  color: #303133;
}

.back-to-top {
  position: fixed;
  bottom: 20px;
  right: 20px;
  z-index: 1000;
  /* 确保按钮在最上层 */
}

.back-to-top el-button {
  padding: 10px;
  background-color: rgba(0, 0, 0, 0.5);
  color: white;
  border: 2px solid blue;
  border-radius: 50%;
}

.back-to-top el-button:hover {
  background-color: rgba(0, 0, 0, 0.7);
}
</style>